package net.sxnic.taglib;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

/**
 * 在页面上根据Map或者list 生成select、radio、checkbox 的Html代码
 * @author 孙宇飞
 *
 */
public class SxnicFormXlistTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;

	// select radio checkbox
	private String cate;
	// 对应的css样式
	private String css;
	// onchange事件
	private String onChange;
	// 在jsp面上显示的名字
	private String name;
	// 预设值 用于回显
	private String value;
	// 默认第一个的key值
	private String headerKey;
	// 默认第一个的value值
	private String headerValue;
	private String childId;
	private String childDataPath;
	// 是否自动适应宽度
	private String autoWidth;
	// 格式：key:value,key2:value2,key3:value3
	private String list;

	private Map<String, String> map;

	public int doStartTag() throws JspException {

		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() {
		JspWriter out = pageContext.getOut();
		try {
			StringBuffer sb = new StringBuffer();
			Map<String, String> bmap = new LinkedHashMap<String, String>();

			if (StringUtils.isBlank(list) && map == null) {
				out.print("list or map need at least one");
				return EVAL_PAGE;
			}

			if (StringUtils.isNotBlank(list)) {
				// list = key:value,key2:value2,key3:value3
				String[] ls = StringUtils.split(list, ",");
				for (String l : ls) {
					bmap.put(StringUtils.substringBefore(l, ":"),
							StringUtils.substringAfter(l, ":"));
				}
			}

			if (map != null && map.size() > 1) {
				bmap = map;
			}

			if ("select".equals(cate)) {

				sb.append("<select id='" + name + "_id' name='" + name + "'");

				if (StringUtils.isNotBlank(onChange)) {
					sb.append(" onchange=\"" + onChange + "\" ");
				}

				if (StringUtils.isNotBlank(css)) {
					sb.append(" class='" + css + "' ");
				}

				if (StringUtils.isNotBlank(childId)) {
					sb.append(" childId='" + childId + "' ");
				}

				if (StringUtils.isNotBlank(childDataPath)) {
					sb.append(" childDataPath='" + childDataPath + "' ");
				}

				if (StringUtils.isNotBlank(autoWidth)) {
					sb.append(" autoWidth='" + autoWidth + "' ");
				}

				sb.append(" >");

				if (StringUtils.isNotBlank(headerKey)
						|| StringUtils.isNotBlank(headerValue)) {
					sb.append("  <option value='" + headerKey
							+ "' selected='selected'>" + headerValue
							+ "</option>");
				}

				Iterator iter = bmap.keySet().iterator();
				while (iter.hasNext()) {
					String infoCode = (String) iter.next();
					if (StringUtils.isNotBlank(value) && infoCode.equals(value)) {
						sb.append("<option value='" + infoCode
								+ "'  selected >" + bmap.get(infoCode)
								+ "</option>");
					} else {
						sb.append("<option value='" + infoCode + "'>"
								+ bmap.get(infoCode) + "</option>");
					}
				}

				sb.append("</select>");
			} else if ("checkbox".equals(cate)) {				
				for (String code : bmap.keySet()) {
					if (StringUtils.contains(value, code)) {
						sb.append("<input type='checkbox' name='" + name
								+ "' value='" + code
								+ "' checked='true' />&nbsp;" + bmap.get(code)
								+ "&nbsp;");
					} else {
						sb.append("<input type='checkbox' name='" + name
								+ "' value='" + code + "' />&nbsp;"
								+ bmap.get(code) + "&nbsp;");
					}
				}
			} else if ("radio".equals(cate)) {
				for (String code : bmap.keySet()) {
					if (StringUtils.contains(value, code)) {
						sb.append("<input type='radio' name='" + name
								+ "' value='" + code
								+ "' checked='true' />&nbsp;" + bmap.get(code)
								+ "&nbsp;");
					} else {
						sb.append("<input type='radio' name='" + name
								+ "' value='" + code + "' />&nbsp;"
								+ bmap.get(code) + "&nbsp;");
					}
				}
			} else {
				sb.append("error cate");
			}

			out.print(sb.toString());

		} catch (IOException e) {

		}

		return EVAL_PAGE;
	}

	public String getCate() {
		return cate;
	}

	public void setCate(String cate) {
		this.cate = cate;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getHeaderKey() {
		return headerKey;
	}

	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	public String getHeaderValue() {
		return headerValue;
	}

	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}

	public String getChildId() {
		return childId;
	}

	public void setChildId(String childId) {
		this.childId = childId;
	}

	public String getChildDataPath() {
		return childDataPath;
	}

	public void setChildDataPath(String childDataPath) {
		this.childDataPath = childDataPath;
	}

	public String getAutoWidth() {
		return autoWidth;
	}

	public void setAutoWidth(String autoWidth) {
		this.autoWidth = autoWidth;
	}

	public String getList() {
		return list;
	}

	public void setList(String list) {
		this.list = list;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

}
